<?php
    /*
        @desc initialization of lib functions
    */

    /*
        @desc lib loading function, load lib from name
        @param string libName name of lib to load
        @return bool status of lib loading function
    */
    function load_lib( $libName, $required = true )
    {
        if (defined( 'SYS_LIB' )) {
            $dName = SYS_LIB . $libName;
            if ( is_dir( $dName ) ) {
                $fName = $dName . '/__init__.php';
                if ( file_exists( $fName ) ) {
                    require_once( $fName );
                    return true;
                }
            }

            //not returned, lib load failure
            if ($required) {
                trigger_error( "Lib ${libName} Not Found!", E_USER_ERROR );
            } else {
                trigger_error( "Lib ${libName} Can not be included!", E_USER_WARNING );
            }
        } else {
            trigger_error( "SYS_LIB Not Defined!", E_USER_ERROR );
        }

        return false;
    }

    /*
        @desc app loading function, load app from name
        @param string appName name of application to load
        @return Object app object instance or false on failure
    */
    function load_app( $appName )
    {
        if ( defined( 'SYS_APP' ) ) {
            $fName = SYS_APP . "${appName}.php";
            if ( file_exists( $fName ) ) {
                require_once( $fName );
                return new $appName;
            }

            //app load failure return false
            trigger_error( "App ${appName} could not be loaded!", E_USER_WARNING );
        } else {
            trigger_error( "SYS_APP Not Defined!", E_USER_ERROR );
        }

        return false;
    }

    /*
        @desc load the config file from config path
        @param string configName name of config file to be loaded
        @return bool status of config file loading
    */
    function load_config( $configName )
    {
        if ( defined( 'SYS_CONFIG' ) ) {
			$fName = SYS_CONFIG . "${configName}.php";
            if ( file_exists( $fName ) ) {
            	require_once( $fName );
                $val = strtolower($configName);
                @$GLOBALS[$val] = ${$val};
                return true;
            }
            
            //return false when config file load failure
            trigger_error( "Config ${configName} could not be loaded!", E_USER_WARNING );
        } else {
            trigger_error( "SYS_CONFIG Not Defined!", E_USER_ERROR );
        }

        return false;
    }
	
    /*
    	@desc load system class no matter where
        @param string of class name, lib or app
        @return Object? class instance object
    */
    function load_class( $className )
    {
        //first try app
        //assert app class valid
        $retVal = @load_app( $className );
        
        if ( $retVal && class_exists( $className ) ) {
        	//return class instance
            return $retVal;
        }
        
        //second try lib
        $retVal = @load_lib( $className );
        
        if ( $retVal && class_exists( $className ) ) {
        	//return lib class instance
        	return new $className;
        }
        
        //trigger warning, class not found
        trigger_error( "Class ${className} could not be loaded!" );
        
        return false;
    }